// pages/details/details.js
const API = require('../../utils/api');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    showInfoContent: true,
    bookindex: [],
    showLimit: 20,
    addedShelf:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var bookId = options.id
    var userId = wx.getStorageSync('id');
    this.setData({
      id: bookId
    })
    API.getbookInfo({
      bookId:bookId,
        userId:userId
    }).then(resp => {
      resp.bookDesc =resp.bookDesc.replace(/<br\/><br\/>/g,'\n')
      this.setData({
        book: resp,
        addedShelf: resp.addShelf
      })
    })
    API.getBookIndex({
      "id": bookId
    }).then(resp => {
      
      this.setData({
        bookindex: resp,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const id = this.data.id;
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  scrollbottom: function (e) {
    var that = this;
    this.setData({
      showLimit: that.data.showLimit + 20
    });
  },
  scrollbottom1: function(e){
    console.log(e)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showTab: function (event) {
    this.setData({
      showInfoContent: !!parseInt(event.target.dataset.id),
      showLimit: 20
    });
  },
  startRead: function (e) {
    const id = this.data.id;
    console.log(id)
    wx.navigateTo({
      url: '../read/index?id=' + id,
    })
  },
  addBookshelf: function(){
    var addShelf =  this.data.addedShelf ;
    var bookId = this.data.book.id
    var userId = wx.getStorageSync('id');
    if(addShelf){
      API.deletebookshelf({
        bookId:bookId,
        userId:userId
      }).then(resp=>{
        this.setData({
          addedShelf:false
        })
      })
    }else{
      API.addbookshelf({
        userId:userId,
        bookId:bookId
      }).then(resp=>{
          this.setData({
            addedShelf:true
          })
        
      })
    }
  }
})